import 'package:flutter/material.dart';

class MagmaTap extends StatefulWidget {
  const MagmaTap({super.key, required this.onTap, required this.child});

  final VoidCallback onTap;
  final Widget child;

  @override
  State<MagmaTap> createState() => _MagmaTapState();
}

class _MagmaTapState extends State<MagmaTap>
    with SingleTickerProviderStateMixin {
  double _scale = 1.0;

  Future<void> _runTapAnimation() async {
    setState(() => _scale = 0.95);
    await Future<void>.delayed(const Duration(milliseconds: 150));
    setState(() => _scale = 1.0);
  }

  void _handleTap() async {
    await _runTapAnimation();
    widget.onTap();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: _handleTap,
      child: AnimatedScale(
        scale: _scale,
        duration: const Duration(milliseconds: 150),
        curve: Curves.easeOut,
        child: widget.child,
      ),
    );
  }
}
